#ifndef _UI_TIMELINE_H_
#define _UI_TIMELINE_H_

#include "math/vec2.h"
#include "math/vec4.h"
#include <vector>
#include <string>
namespace LuaPlus
{
    class LuaObject;
}

namespace ui
{
class UIWidget; 
class UITimeLine
{
public:
	struct			Keyframe
	{
		float					Time;			// time in the keyframe
		math::Vec2				Pos;			// position of top-left corner
		math::Vec2				Size;			// extents of box
		math::Vec2				UV[2];			// Top left / bottom right UV
		math::Vec4				Color;			// Color ARGB
		float					Rotation;		// Rotation 
		float					Scale;			// Scale of self and children
		std::string				Script;			// script to execute on this frame
	};
								UITimeLine(UIWidget* pOwner);
								~UITimeLine();

	bool						Load(LuaPlus::LuaObject& _LObj);	// load from lua

	/// add a new keyframe 
	void						AddKeyframe(float t, const math::Vec2& Pos, const math::Vec2& Size, 
											const math::Vec4& Color = math::Vec4(), 
											float Rot = 0.0f, float Scale = 1.0f, 
											const math::Vec2& UV1 = math::Vec2(), 
											const math::Vec2& UV2 = math::Vec2(), 
											const std::string& Script = "");
	/// sorts the keyframes and initializes boundry values
	void						Finalize();

	void						Tick(float dt);
	float						GetTime();
	void						SetTime(float t);
	
	// Accessors for a given time
	const math::Vec2&			GetPos();			// Position of top left corner
	const math::Vec2&			GetSize();			// extents of box
	const math::Vec4&			GetColor();			// Color ARGB
	const math::Vec2&			GetUV1();			// Top left UV
	const math::Vec2&			GetUV2();			// Bottom right UV
	float						GetRotation();		// Rotation 
	float						GetScale();			// Scale of self and children
	
private:
	void						BuildKeyframe();
	void						ExecuteScript(Keyframe*	pKey);

	std::vector<Keyframe*>		m_Keyframes;	// keyframes
	float						m_MinTime;		// first keyframe
	float						m_MaxTime;		// last keyframe
	float						m_Time;
	int							m_CurrentKey;	// last key we passed
	Keyframe					m_CurrentKeyframe;	// interpolation between current and next
	UIWidget*					m_pOwner;
};

} // namespace ui

#endif // _UI_TIMELINE_H_